package com.zygh.lz.security;

import com.zygh.lz.entity.Role;
import com.zygh.lz.entity.Staff;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class CustomUserDetails implements UserDetails {
    private String username;
    private String staffPassword;
    private List<Role> roleList;

    public CustomUserDetails(String username, String staffPassword, List<Role> list) {
        this.username = username;
        this.staffPassword = staffPassword;
        this.roleList = list;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //返回当前用户的权限
        return roleList.stream()
                .filter(role -> role.getSubsystemUsetype() != null)
                .map(role -> new SimpleGrantedAuthority(role.getMenuUsetype()))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return this.staffPassword;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public String toString() {
        return this.username;
    }
    @Override
    public int hashCode() {
        return username.hashCode();
    }
    @Override
    public boolean equals(Object obj) {
        return this.toString().equals(obj.toString());
    }



}
